package com.shqcjd.preinstallglass.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.shqcjd.preinstallglass.R;
import com.shqcjd.preinstallglass.api.ApiService;
import com.shqcjd.preinstallglass.helper.rxjavahelper.RxObserver;
import com.shqcjd.preinstallglass.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.preinstallglass.model.ResponseData;
import com.shqcjd.preinstallglass.model.eventbus.CreateVideotape;
import com.shqcjd.preinstallglass.model.pojo.EquipmentGroupBean;
import com.shqcjd.preinstallglass.ui.adapter.InspectionStandardAdapter;
import com.shqcjd.preinstallglass.ui.base.BaseActivity;
import com.shqcjd.preinstallglass.ui.base.BasePresenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class InspectionStandardActivity extends BaseActivity {

    @BindView(R.id.rv_inspection_standard)
    RecyclerView rvInspectionStandard;
    @BindView(R.id.btn_finish)
    public Button btnFinish;

    @BindView(R.id.btn_equipment)
    public Button btnEquipment;


    private Context context;
    private String kks;
    private int tId;
    private int epId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_inspection_standard;
    }

    @Override
    public void init() {
        super.init();
        kks = getIntent().getStringExtra("kks");
        tId = getIntent().getIntExtra("tId", -1);
    }

    @Override
    public void initView() {
        super.initView();
        context = this;
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnEquipment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(context, EquipmentGroupsActivity.class);
                it.putExtra("kks", kks);
                it.putExtra("tId", tId);
                it.putExtra("epId", epId);
                startActivity(it);
                finish();
            }
        });
        getData();
    }

    private void getData() {
        ApiService.equipmentGroupHttp(tId, kks.replace("#", "%23"))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<EquipmentGroupBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<EquipmentGroupBean> responseData) {
                        if(responseData.retCode!=10000){
                            Toast.makeText(context, responseData.retMsg, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        rvInspectionStandard.setLayoutManager(new LinearLayoutManager(context));
                        InspectionStandardAdapter inspectionStandardAdapter = new InspectionStandardAdapter(responseData.data.equipment_standard, context);
                        rvInspectionStandard.setAdapter(inspectionStandardAdapter);
                        EquipmentGroupBean data = responseData.data;
                        //开启全程录像
                        EventBus.getDefault().post(new CreateVideotape(InspectionStandardActivity.this,kks,tId+""));
                        epId = data.equipment_id;
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }

                });
    }
}
